<?php
namespace Biz\pockets\excel;

class UpExcel
{
    public static $error = '';//定义错误的文本
    public $fileName = ''; //生成文件名
    private $type = array('application/vnd.ms-excel', 'application/octet-stream', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//定义属性
    private $max = 12;//定义兆数
    private $maxSize = 12582912;// 1024*1024*12
    private $path = '/tmp/';//文件根目录

    /**
     * 定义单文件上传方法
     * @param $file
     * @return bool|string
     */
    function uploadOne($file)
    {
        $this->fileName = $file['name'];
        $res1 = $this->checkType($file['type']);
        $res2 = $this->checkError($file['error']);
        $res3 = $this->checkSize($file['size']);
        if ($res1 && $res2 && $res3) {
            return $file;
        } else {
            return false;
        }
    }

    /**
     * 验证文件的错误级别
     * @param $fileError
     * @return bool
     */
    private function checkError($fileError)
    {
        switch ($fileError) {
            case 0:
                return true;
            case 1:
                self::$error = '上传的文件超过了php.ini中的设置';
                return false;
            case 2:
                self::$error = '上传的文件超过了form表单中的设置';
                return false;
            case 3:
                self::$error = '只有部分文件被上传';
                return false;
            case 4:
                self::$error = '没有文件被上传';
                return false;
            case 6:
                self::$error = '找不到临时文件';
                return false;
            case 7:
                self::$error = '文件写入失败';
                return false;
        }
    }

    /**
     * 验证文件的类型
     * @param $fileType
     * @return bool
     */
    private function checkType($fileType)
    {
        if (!in_array($fileType, $this->type)) {
            self::$error = '文件的格式只支持excel格式';
            return false;
        } else {
            return true;
        }
    }

    /**
     * 验证文件的大小
     * @param $fileSize
     * @return bool
     */
    private function checkSize($fileSize)
    {
        if ($fileSize > $this->maxSize) {
            self::$error = '请上传' . $this->max . 'M以内的文件';
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取文件全路径
     * @param $fileName
     * @return string
     * @throws \Exception
     */
    private function getPath($string)
    {
        //修改文件的名称  拼接当前的时间戳+4位的数字和字母随机数
        $arr = strtoupper('a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z');
        $data = explode(',', $arr);
        $fileType = $this->getExt();
        $this->fileName = time() . '_' . rand(1, 9) . $data[rand(0, 25)] . rand(1, 9) . $data[rand(0, 25)] . (empty($string) ?: '_' . $string) . '.' . $fileType;

        //检测文件上传的路径是否存在  不存在则创建
        if (!file_exists($this->path)) {
            if (!mkdir($this->path, 0755, true)) {
                throw new \Exception('文件夹创建失败');
            }
        }
        return $this->path . $this->fileName;
    }

    /**
     * 获取文件后缀名
     * @param $fileName
     * @return bool|string
     * @throws \Exception
     */
    private function getExt()
    {
        $pos = mb_strrpos($this->fileName, '.');
        if ($pos === false) {
            throw new \Exception('请检查文件后缀名是否配置');
        }

        return mb_substr($this->fileName, $pos + 1);
    }

    /**
     * 多文件上传
     * @param $file
     * @return bool
     */
    function uploadMany($file)
    {
        $m = 0;
        foreach ($file['name'] as $k => $v) {
            $this->fileName = $file['name'][$k];
            $res1 = $this->checkType($file['type'][$k]);
            $res2 = $this->checkError($file['error'][$k]);
            $res3 = $this->checkSize($file['size'][$k]);
            if ($res1 && $res2 && $res3) {
                $data['path'][$k] = $v['tmp_name'];
                $m++;
            } else {
                return false;
            }
        }
        if (count($data['path']) == $m) {
            return $data;
        }
    }
}

?>
